var $table = $('#table');

var $caddbtn = $("#cadd");
var $queryBtn = $('#queryBtn');



$(function () {



    $('.form-control-chosen').chosen({
        allow_single_deselect: true,
        width: '100%'
    });








    $caddbtn.click(function () {
        var searchvaleimtablename = $("#searchString_id").val();
        var searchvalmodelname= $("#searchString_corid").val();
        var info = $table.bootstrapTable('getSelections');
        var ids = "";
        for (let i = 0; i < info.length; i++) {
            ids += info[i].conflict_id + ",";

        }

        if (info.length < 1) {
            rapid.info("请至少选择一行!");
        } else {
            rapid.post({
                url: "/sysadmin/blockchain/addHashBlockchain?eimtablename="+searchvaleimtablename+"&modelname="+searchvalmodelname,
                data: "ids=" + ids,
                success: function (result) {

                    loadTalbe($table)
                }
            });
        }

    });

    function loadTalbe(table){
        table.bootstrapTable('destroy');  // 销毁原表格

        var searchvaleimtablename = $("#searchString_id").val();
        var searchvalmodelname= $("#searchString_corid").val();
        if(searchvaleimtablename!=null&&searchvaleimtablename!=""&&searchvalmodelname!=null&&searchvalmodelname!=""){
            inittab();
            table.bootstrapTable('refresh', {
        query: {
            eimtablename: searchvaleimtablename,
            modelname: searchvalmodelname,

            pageIndex: 1,
            pageSize:1
        }
         });
}

    }

    $queryBtn.click(function () {

        loadTalbe($table)
    });





    function changeDateFormat(cellval) {
        var val=cellval+"";

        if( val != '' && val !=null && val !='null' &&  val!='undefined')
        {
            return val.substring(0,10)
        }

    }



    function inittab() {
        $table.bootstrapTable({
            url: "/sysadmin/blockchain/getblockchains",
            idField: 'conflict_id',
            dataType: 'json',
            sidePagination: "server", //服务端处理分页
            queryParamsType: "undefined",//设置为 ‘limit’ 则会发送符合 RESTFul 格式的参数.
            queryParams: function (params) {//自定义参数，这里的参数是传给后台的，我这是是分页用的


                return {//这里的params是table提供的
                    pageIndex: params.pageNumber,//从数据库第几条记录开始
                    pageSize: params.pageSize,//找多少条
                    eimtablename: $("#searchString_id").val(),
                    modelname: $("#searchString_corid").val(),

                }

            },
            responseHandler: function (res) {

                var result = {total: 0, rows: []};

                if (res.recordCount > 0) {
                    result.total = 1;
                    result.rows = res.list;
                }
                return result;
            },
            sortable: true,

            pagination: 'true',  //开启分页
            paginationPreText: '上一页',
            paginationNextText: '下一页',
            // singleSelect: true, //单选

            showColumns: true,
            clickToSelect: true,
            showRefresh: true,
            columns: [
                {
                    field: 'check', checkbox: true, formatter: function (value, row, index) {

                        if (row.check == true) {

                            return {checked: true};
                        }
                    }
                },
                {field: 'attrib_04', title: '姓名', width: 300, class: 'colStyle'},

                {field: 'attrib_03', title: '身份证号码', class: 'colStyle'},
                {field: 'attrib_05', title: '手机号码', class: 'colStyle'},
                {field: 'attrib_02', title: '标签', class: 'colStyle'}
            ]


        });
    }
    $(document).ready(function(){
        inittab();
    });

})
